package com.no.hq.controller.user;

import com.github.pagehelper.PageInfo;
import com.no.hq.pojo.User;
import com.no.hq.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("toupdate/{id}")
    public String toUpdate(@PathVariable Long id, Model model) {
        log.info("准备导航到修改用户信息视图......");
        User user = userService.queryById(id);
        model.addAttribute("user", user);
        return "user/update";
    }

    @RequestMapping("update")
    public String update(User user) {
        log.info("接受到用户的修改请求，传递过来的数据是：user -> " + user);
        userService.modifyUser(user);
        return "redirect:/user/show?id=" + user.getId();
    }

    @RequestMapping("show")
    public String show(@RequestParam Long id, Model model) {
        log.info("接收到修改模块传递过来的用户id -> " + id);
        // 根据id查询到相应的数据，
        User user = userService.queryById(id);
        // 封装之后
        model.addAttribute("user", user);
        // ，导航
        return "user/show";
    }

    // 导航到新添页面的方法
    @RequestMapping("toinsert")
    public String toInsert() {
        log.debug("准备跳转到新用户添加页...");
        return "user/insert";
    }

    // 新添
    @RequestMapping("insert")
    public String insert(User user) {
        log.debug("拿到前台传递过来的数据 user -> " + user);
        userService.saveUser(user);
        return "redirect:/user/show/" + user.getId();
    }

    // 根据id删除user
    @RequestMapping("delete/{id}")
    public String deleteById(@PathVariable Long id) {

        log.debug("准备执行删除业务，删除id为 " + id + "的用户");
        userService.deleteById(id);
        return "redirect:/user/showAll";
    }

    // 查找所有
    @RequestMapping("showAll")
    public String showAll(Model model) {
        log.debug("从后台获取到全部数据，跳转到用户列表页...");
        List<User> userList = userService.queryAll();
        model.addAttribute("userList", userList);
        return "user/showAll";
    }

    //@Value("${page.size}")
    String pageSize;
    @RequestMapping("queryByPage/{pageNum}")
    public String queryByPage(@PathVariable("pageNum") Integer pageNum, Model model) {
        if(pageNum < 1)
            pageNum = 1;
        PageInfo<User> pageInfo = userService.queryByPage(pageNum, Integer.valueOf(pageSize));
        model.addAttribute("pageInfo", pageInfo);
        return "user/showByPage";
    }

    @InitBinder
    public void initBinder(ServletRequestDataBinder binder) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(sdf, true));
    }

}
